package com.mingweicai.intergration.redis.cluster.configuration;

import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

//@Configuration
public class RedisClusterConfig {

  private Logger logger = LoggerFactory.getLogger(RedisClusterConfig.class);

  @Bean
  public JedisCluster jedisCluster(
      RedisClusterProperties redisClusterProperties,
      JedisPoolConfig jedisPoolConfig
      ){
    logger.debug("JedisPoolConfig is : {}",jedisPoolConfig);
    Set<HostAndPort> hostAndPorts = new HashSet<>();
    for(String hostAndPort : redisClusterProperties.getClusterNodes()){
      String arr[] = hostAndPort.split(":");
      hostAndPorts.add(new HostAndPort(arr[0],Integer.valueOf(arr[1])));
    }

    return new JedisCluster(hostAndPorts,5000,1000,3,"123456",jedisPoolConfig);

  }

}
